﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Waiter.Common;

namespace Waiter.Controler
{
    class OrderCouponCtrl
    {
        public List<ORDER_COUPON_D> fsGetCurrentOrderCouponList(string tableCode)
        {
            List<ORDER_COUPON_D> resultList = null;
            try
            {
                resultList = (from s in CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds
                              where s.DEL_FLG == CommonConstants.FLAG_OFF
                                && s.ORDER_COUPON.TABLE_CODE == tableCode
                                && s.ORDER_COUPON.STATUS == (int)OrderCouponStatus.NOT_CHARGED
                              select s).ToList();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsGetCurrentOrderCouponList",
                    CommonUtils.fsParamListToString(new object[1] { tableCode }),
                    ex.ToString());
            }
            return resultList;
        }

        public ResultStatus fsHRemoveAllCurrentOrderCouponDetail(string tableCode)
        {
            try
            {
                List<ORDER_COUPON_D> resultList = (from s in CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds
                              where s.DEL_FLG == CommonConstants.FLAG_OFF
                                && s.ORDER_COUPON.TABLE_CODE == tableCode
                                && s.ORDER_COUPON.STATUS == (int)OrderCouponStatus.NOT_CHARGED
                              select s).ToList();
                if (resultList != null)
                {
                    CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds.DeleteAllOnSubmit(resultList);
                    CommonGlobalVariable.globalDataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsGetCurrentOrderCouponList",
                    CommonUtils.fsParamListToString(new object[1] { tableCode }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsHRemoveOrderCouponDetail(string tableCode, string menuCode)
        {
            try
            {
                List<ORDER_COUPON_D> resultList = (from s in CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds
                                                   where s.DEL_FLG == CommonConstants.FLAG_OFF
                                                     && s.ORDER_COUPON.TABLE_CODE == tableCode
                                                     && s.ORDER_COUPON.STATUS == (int) OrderCouponStatus.NOT_CHARGED
                                                     && s.MENU_CODE == menuCode
                                                   select s).ToList();
                if (resultList != null)
                {
                    CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds.DeleteAllOnSubmit(resultList);
                    CommonGlobalVariable.globalDataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsHRemoveOrderCouponDetail",
                    CommonUtils.fsParamListToString(new object[2] { tableCode, menuCode }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }
        public ResultStatus fsHRemoveCurrentOrderCoupon(ORDER_COUPON item)
        {
            try
            {
                fsHRemoveAllCurrentOrderCouponDetail(item.TABLE_CODE);
                CommonGlobalVariable.globalDataContext.ORDER_COUPONs.DeleteOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsHRemoveCurrentOrderCoupon",
                    CommonUtils.fsParamListToString(new object[1] { item }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }
        public ResultStatus fsHRemoveCurrentOrderCoupon(string tableCode)
        {
            try
            {
                fsHRemoveAllCurrentOrderCouponDetail(tableCode);
                ORDER_COUPON orderCoupon = CommonGlobalVariable.globalDataContext
                    .ORDER_COUPONs.First(q => q.DEL_FLG == CommonConstants.FLAG_OFF
                        && q.TABLE_CODE == tableCode && q.STATUS == (int)OrderCouponStatus.NOT_CHARGED);
                if (orderCoupon != null)
                {
                    CommonGlobalVariable.globalDataContext.ORDER_COUPONs.DeleteOnSubmit(orderCoupon);
                    CommonGlobalVariable.globalDataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsHRemoveCurrentOrderCoupon",
                    CommonUtils.fsParamListToString(new object[1] { tableCode }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ORDER_COUPON fsGetCurrentOrderCoupon(string tableCode)
        {
            try
            {
                ORDER_COUPON orderCoupon = CommonGlobalVariable.globalDataContext
                    .ORDER_COUPONs.First(q => q.DEL_FLG == CommonConstants.FLAG_OFF
                        && q.TABLE_CODE == tableCode && q.STATUS == (int)OrderCouponStatus.NOT_CHARGED);
                return orderCoupon;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsGetCurrentOrderCoupon",
                    CommonUtils.fsParamListToString(new object[1] { tableCode }),
                    ex.ToString());
                return null;
            }
        }

        public ResultStatus fsInsertOrderCoupon(ORDER_COUPON item)
        {
            try
            {
                CommonGlobalVariable.globalDataContext.ORDER_COUPONs.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsInsertOrderCoupon",
                    CommonUtils.fsParamListToString(new object[1] { item }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsInsertOrderCouponDetail(List<ORDER_COUPON_D> itemList)
        {
            try
            {
                CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds.InsertAllOnSubmit(itemList);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsInsertOrderCouponDetail",
                    CommonUtils.fsParamListToString(itemList.ToArray()),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsInsertOrderCouponDetail(ORDER_COUPON_D item)
        {
            try
            {
                CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsInsertOrderCouponDetail",
                    CommonUtils.fsParamListToString(new object[1] { item }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsInsertOrUpdateOrderCoupon(string tableCode, string menuCode, int count)
        {
            MenuCtrl menuCtrl = new MenuCtrl();
            TableCtrl tableCtrl = new TableCtrl();
            TABLE tableItem = tableCtrl.fsFindTableByCode(tableCode);
            try
            {
                ORDER_COUPON orderCoupon = fsGetCurrentOrderCoupon(tableCode);
                if (orderCoupon == null)
                {
                    tableItem.STATUS_CODE = (int) TableStatus.BUSY;
                    orderCoupon = new ORDER_COUPON();
                    orderCoupon.CODE = CommonUtils.fsCreateNewCode();
                    orderCoupon.TABLE_CODE = tableCode;
                    orderCoupon.STATUS = (int) OrderCouponStatus.NOT_CHARGED;
                    orderCoupon.DEL_FLG = CommonConstants.FLAG_OFF;
                    orderCoupon.OPID = CommonUtils.fsGetOPID();
                    orderCoupon.ADD_DATE = DateTime.Now;
                    orderCoupon.UPD_DATE = DateTime.Now;
                    fsInsertOrderCoupon(orderCoupon);
                }
                ORDER_COUPON_D orderCouponD = (from s in CommonGlobalVariable.globalDataContext.ORDER_COUPON_Ds
                            where s.COUPON_CODE == orderCoupon.CODE && s.MENU_CODE == menuCode
                            select s).FirstOrDefault();
                if (orderCouponD == null)
                {
                    orderCouponD = new ORDER_COUPON_D();
                    orderCouponD.COUPON_CODE = orderCoupon.CODE;
                    orderCouponD.MENU_CODE = menuCode;
                    orderCouponD.COUNT = count;
                    orderCouponD.STATUS = (int)OrderCouponDStatus.NOT_PREPARED;
                    orderCouponD.PRICE = menuCtrl.fsGetSalesPrice(menuCode, tableItem.AREA_CODE);
                    orderCouponD.DEL_FLG = CommonConstants.FLAG_OFF;
                    orderCouponD.OPID = CommonUtils.fsGetOPID();
                    orderCouponD.ADD_DATE = DateTime.Now;
                    orderCouponD.UPD_DATE = DateTime.Now;
                    fsInsertOrderCouponDetail(orderCouponD);
                }
                else
                {
                    orderCouponD.COUNT += count;
                    orderCouponD.PRICE = menuCtrl.fsGetSalesPrice(menuCode, tableItem.AREA_CODE);
                    orderCouponD.OPID = CommonUtils.fsGetOPID();
                    orderCouponD.UPD_DATE = DateTime.Now;
                    CommonGlobalVariable.globalDataContext.SubmitChanges();
                }

            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsInsertOrderCoupon",
                    CommonUtils.fsParamListToString(new object[3] { tableCode, menuCode, count }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsRemoveOrUpdateOrderCoupon(string tableCode, string menuCode)
        {
            MenuCtrl menuCtrl = new MenuCtrl();
            TableCtrl tableCtrl = new TableCtrl();
            TABLE tableItem = tableCtrl.fsFindTableByCode(tableCode);
            try
            {
                fsHRemoveOrderCouponDetail(tableCode, menuCode);
                ORDER_COUPON orderCoupon = fsGetCurrentOrderCoupon(tableCode);
                if (orderCoupon.ORDER_COUPON_Ds == null || orderCoupon.ORDER_COUPON_Ds.Count == 0)
                {
                    tableItem.STATUS_CODE = (int)TableStatus.EMPTY;
                    fsHRemoveCurrentOrderCoupon(tableCode);
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsRemoveOrUpdateOrderCoupon",
                    CommonUtils.fsParamListToString(new object[2] { tableCode, menuCode}),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsChangeAllOrderCoupon(string table1, string table2)
        {
            try
            {
                TableCtrl tableCtrl = new TableCtrl();
                string areaCode = tableCtrl.fsFindTableByCode(table2).AREA_CODE;
                MenuCtrl menuCtrl = new MenuCtrl();
                ORDER_COUPON orderCoupon1 = fsGetCurrentOrderCoupon(table1);
                ORDER_COUPON orderCoupon2 = fsGetCurrentOrderCoupon(table2);
                if (orderCoupon2 == null)
                {
                    orderCoupon1.TABLE_CODE = table2;
                    CommonGlobalVariable.globalDataContext.SubmitChanges();

                    List<ORDER_COUPON_D> list = fsGetCurrentOrderCouponList(table2);
                    foreach (ORDER_COUPON_D item in list)
                    {
                        item.PRICE = menuCtrl.fsGetSalesPrice(item.MENU_CODE, areaCode);
                    }
                    CommonGlobalVariable.globalDataContext.SubmitChanges();
                }
                else
                {
                    List<ORDER_COUPON_D> itemList = orderCoupon1.ORDER_COUPON_Ds.ToList();
                    foreach (ORDER_COUPON_D item in itemList)
                    {
                        item.DEL_FLG = CommonConstants.FLAG_ON;

                        fsInsertOrUpdateOrderCoupon(table2, item.MENU_CODE, item.COUNT);
                    }
                    orderCoupon1.DEL_FLG = CommonConstants.FLAG_ON;
                    CommonGlobalVariable.globalDataContext.SubmitChanges();
                }

                tableCtrl.fsUpdateTableStatus(table1, (int)TableStatus.EMPTY);
                tableCtrl.fsUpdateTableStatus(table2, (int)TableStatus.BUSY);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsChangeAllOrderCoupon",
                    CommonUtils.fsParamListToString(new object[2] { table1, table2 }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsUpdateStatus(string code, int status)
        {
            try
            {
                ORDER_COUPON item = CommonGlobalVariable.globalDataContext.ORDER_COUPONs.First(q => q.CODE == code);
                item.STATUS = status;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "OrderCouponCtrl.fsUpdateStatus",
                    CommonUtils.fsParamListToString(new object[2] { code, status }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }
    }
}
